package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.Animatable
import android.net.Uri
import android.os.Build
import android.text.Html
import android.text.SpannableString
import android.text.style.LeadingMarginSpan
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.imagepipeline.image.ImageInfo
import com.lancoo.znbkxx.teacher.model.data.ChildOption
//import com.lancoo.znbkxx.teacher.model.data.OptionX
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_question_choice.view.*
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.regex.Pattern


/**
 * @author : Admin
 * @date   : 2019/9/2 17:24
 * @desc   :
 */
class ChoiceAdapter(
    val allCount: Int,
    val type: String?,
    layoutResId: Int,
    data: MutableList<ChildOption>?
) : BaseQuickAdapter<ChildOption, BaseViewHolder>(layoutResId, data) {
    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: ChildOption?) {
        with(helper.itemView) {
            val content = item!!.Option.trim()
            val percent = if (type != "x" && type != "k")
                "(${item.StuCount}，约${item.Rate})" else ""
            if (content.contains("src")) {
                tv_choice.text = "${item.OptionIndex}."
                sdv.visibility = View.VISIBLE
                val matcher = Pattern.compile(
                    "((http|ftp|https)://)(([a-zA-Z0-9._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/?[a-zA-Z0-9&%_./-~-]*)?",
                    Pattern.CASE_INSENSITIVE
                ).matcher(content)
                while (matcher.find()) {
                    val path = matcher.group(0)
                    val controllerListener =
                        Fresco.newDraweeControllerBuilder().setControllerListener(object :
                            BaseControllerListener<ImageInfo>() {
                            override fun onFinalImageSet(
                                id: String?,
                                imageInfo: ImageInfo?,
                                animatable: Animatable?
                            ) {
                                val params =
                                    sdv.layoutParams
                                params.height = DensityUtils.dp2px(context, 100f)
                                params.width =
                                    ((imageInfo!!.width.toFloat() / imageInfo.height * DensityUtils.dp2px(
                                        context,
                                        100f
                                    )).toInt())
                                sdv.layoutParams = params
                            }
                        }).setUri(Uri.parse(path)).build()
                    sdv.controller = controllerListener
                }
                if (type != "x" && type != "m") {
                    tv_num.visibility = View.VISIBLE
                } else {
                    tv_num.visibility = View.GONE
                }
                tv_num.text = percent
            } else {
                if (type != "x") {
                    if (content.contains("<")) {
                        tv_choice.text =
                            HtmlUtils.setText("${item.OptionIndex}.${content}  $percent")
                    } else {
                        tv_choice.text = "${item.OptionIndex}.${content}  $percent"
                    }
                } else {
                    tv_choice.text = "${item.OptionIndex}.${content}"
                }
                val spannable = SpannableString(tv_choice.text)
                val what = LeadingMarginSpan.Standard(0, DensityUtils.dp2px(context, 15f))
                spannable.setSpan(
                    what,
                    0,
                    spannable.length,
                    SpannableString.SPAN_INCLUSIVE_INCLUSIVE
                )
                tv_choice.text = spannable
                sdv.visibility = View.GONE
                tv_num.visibility = View.GONE
            }
            if (item.isAnswer && type != "k") {
                tv_choice.setTextColor(Color.parseColor("#1ab600"))
                tv_num.setTextColor(Color.parseColor("#1ab600"))
            } else {
                tv_choice.setTextColor(Color.parseColor("#333333"))
                tv_num.setTextColor(Color.parseColor("#333333"))
            }
        }
    }
}